package com.savitar.dict.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.savitar.common.vo.ResultBean;
import com.savitar.dict.entity.Dict;
import com.savitar.dict.entity.DictType;
import com.savitar.dict.service.DictService;
import com.savitar.dict.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (Dict)表控制层
 *
 * @author Savitar
 * @since 2021-05-19 21:17:48
 */
@RestController
@RequestMapping("dict")
public class DictController extends ApiController {
    /**
     * 服务对象
     */
    @Autowired
    private DictService service;

    @GetMapping("/page/{typeId}")
    public ResultBean page(@PathVariable String typeId, @RequestParam Integer pageNum, @RequestParam Integer pageSize,
                           @RequestParam(value = "key", required = false) String key
                           ) {
        try {
            return ResultBean.ok(service.page(pageNum, pageSize, typeId,key));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PostMapping
    public ResultBean create(@RequestBody Dict createVo) {
        try {
            return ResultBean.ok(service.create(createVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PutMapping
    public ResultBean update(@RequestBody Dict updateVo) {
        try {
            return ResultBean.ok(service.update(updateVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultBean deleteById(@PathVariable String id) {
        try {
            return ResultBean.ok(service.deleteById(id));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @GetMapping("/listByType/{type}")
    public ResultBean listByType(@PathVariable String type) {
        try {
            return ResultBean.ok(service.listByType(type));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }
}